package cn.dai.wechat.method;

import cn.dai.wechat.config.WechatConfig;
import cn.dai.wechat.config.WechatTokenConfig;
import com.alibaba.fastjson.JSONObject;
import cn.dai.util.HttpUtil;
import cn.dai.wechat.pojo.WechatTokenDto;
import lombok.extern.slf4j.Slf4j;

import java.util.HashMap;
import java.util.Map;

/**
 * @author daijiyong
 * 通讯录管理-用户管理api接口
 */
@Slf4j
public class WechatContactUser {

    public String wechatContactUserUrl = "https://qyapi.weixin.qq.com/cgi-bin/user";

    public final String corpSecret = "-";


    /**
     * 获取某部门下用户详情列表
     *
     * @param departmentId 部门id，所有用户列表则id=1
     * @return json实体
     */
    public JSONObject list(String departmentId) {
        try {
            Map<String, String> params = new HashMap<>();
            WechatTokenDto token = WechatTokenConfig.getWechatToken(WechatConfig.CORP_ID, corpSecret);
            params.put("access_token", token.getToken());
            params.put("department_id", departmentId);
            String result = HttpUtil.doGet(wechatContactUserUrl + "/list", params);
            log.info("获取用户信息成功：{}", result);
            return JSONObject.parseObject(result);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
